/*
 *
 * Copyright (C) 2012 Walter Lütgenau
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */
package offset.nodes.client.editor.view;

import offset.nodes.client.editor.model.TableModel;

/**
 *
 * @author  Walter Lütgenau
 */
public class InsertTableDialog extends javax.swing.JDialog {
	public static final int OK = 1;
	public static final int CANCEL = 2;
	
	int rc = CANCEL;
    
    /** Creates new form InsertTableDialog */
    public InsertTableDialog(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        getRootPane().setDefaultButton( okButton );
    }
    
    public int showDialog() {
    	show();
    	return rc;
    }
    
    public int getColumnCount() {
    	return ((Integer)columnCountSpinner.getValue()).intValue();
    }

    public int getRowCount() {
        return ((Integer)rowCountSpinner.getValue()).intValue();
    }
    
    public int getColumnWidthType() {
    	if ( fixedButton.isSelected())
    		return TableModel.COLUMN_WIDTH_TYPE_FIXED;
    	else if ( fixedPercentButton.isSelected())
    		return TableModel.COLUMN_WIDTH_TYPE_FIXED_PERCENT;
    	else 
    		return TableModel.COLUMN_WIDTH_TYPE_VARIABLE;
    }

    public boolean headerLabels() {
        return headerLabelsCheckBox.isSelected();
    }

    public boolean columnLabels() {
        return columnLabelsCheckBox.isSelected();
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        columnWidthTypeGroup = new javax.swing.ButtonGroup();
        mainPanel = new javax.swing.JPanel();
        okButton = new javax.swing.JButton();
        cancelButton = new javax.swing.JButton();
        columnCountSpinner = new javax.swing.JSpinner();
        jPanel1 = new javax.swing.JPanel();
        columnCountSeparatorPanel = new javax.swing.JPanel();
        columnCountLabel = new javax.swing.JLabel();
        columnCountSeparator = new javax.swing.JSeparator();
        countLabel = new javax.swing.JLabel();
        columnWidthSeparatorPanel = new javax.swing.JPanel();
        columnWidthLabel1 = new javax.swing.JLabel();
        columnWidthSeparator1 = new javax.swing.JSeparator();
        fixedButton = new javax.swing.JRadioButton();
        fixedPercentButton = new javax.swing.JRadioButton();
        variableButton = new javax.swing.JRadioButton();
        labelsLabel = new javax.swing.JLabel();
        labelsSeparator = new javax.swing.JSeparator();
        headerLabelsCheckBox = new javax.swing.JCheckBox();
        columnLabelsCheckBox = new javax.swing.JCheckBox();
        rowsLabel = new javax.swing.JLabel();
        rowCountSpinner = new javax.swing.JSpinner();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Insert table");

        mainPanel.setPreferredSize(new java.awt.Dimension(400, 270));

        java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle("offset/nodes/client/editor/view/resources/EditorBundle"); // NOI18N
        okButton.setText(bundle.getString("Button.ok")); // NOI18N
        okButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ok(evt);
            }
        });

        cancelButton.setText(bundle.getString("Button.cancel")); // NOI18N
        cancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancel(evt);
            }
        });

        columnCountSpinner.setPreferredSize(new java.awt.Dimension(40, 20));
        columnCountSpinner.setValue(new Integer(2));

        columnCountSeparatorPanel.setLayout(new java.awt.GridBagLayout());

        columnCountLabel.setText(bundle.getString("table.insertTable.columnCount")); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        columnCountSeparatorPanel.add(columnCountLabel, gridBagConstraints);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(0, 5, 0, 0);
        columnCountSeparatorPanel.add(columnCountSeparator, gridBagConstraints);

        countLabel.setText(bundle.getString("table.insertTable.columnCount")); // NOI18N

        columnWidthSeparatorPanel.setLayout(new java.awt.GridBagLayout());

        columnWidthLabel1.setText(bundle.getString("table.insertTable.columnWidthType")); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        columnWidthSeparatorPanel.add(columnWidthLabel1, gridBagConstraints);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(0, 5, 0, 0);
        columnWidthSeparatorPanel.add(columnWidthSeparator1, gridBagConstraints);

        columnWidthTypeGroup.add(fixedButton);
        fixedButton.setSelected(true);
        fixedButton.setText("Fixed");

        columnWidthTypeGroup.add(fixedPercentButton);
        fixedPercentButton.setText("Fixed (%)");

        columnWidthTypeGroup.add(variableButton);
        variableButton.setText("Variable");

        labelsLabel.setText(bundle.getString("table.insertTable.labelsPosition")); // NOI18N

        headerLabelsCheckBox.setText(bundle.getString("table.insertTable.labelsTop")); // NOI18N

        columnLabelsCheckBox.setText(bundle.getString("table.insertTable.columns")); // NOI18N

        rowsLabel.setText(bundle.getString("table.insertTable.rowCount")); // NOI18N

        rowCountSpinner.setPreferredSize(new java.awt.Dimension(40, 20));
        rowCountSpinner.setValue(new Integer(2));

        javax.swing.GroupLayout mainPanelLayout = new javax.swing.GroupLayout(mainPanel);
        mainPanel.setLayout(mainPanelLayout);
        mainPanelLayout.setHorizontalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(mainPanelLayout.createSequentialGroup()
                .addGap(10, 10, 10)
                .addComponent(countLabel)
                .addGap(5, 5, 5)
                .addComponent(columnCountSpinner, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(32, 32, 32)
                .addComponent(rowsLabel)
                .addGap(5, 5, 5)
                .addComponent(rowCountSpinner, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(9, 9, 9))
            .addGroup(mainPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(columnCountSeparatorPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE)
                .addContainerGap())
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, mainPanelLayout.createSequentialGroup()
                .addContainerGap(269, Short.MAX_VALUE)
                .addComponent(okButton)
                .addGap(5, 5, 5)
                .addComponent(cancelButton)
                .addContainerGap())
            .addGroup(mainPanelLayout.createSequentialGroup()
                .addGap(9, 9, 9)
                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(columnWidthSeparatorPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 381, Short.MAX_VALUE)
                    .addComponent(fixedButton)
                    .addComponent(fixedPercentButton)
                    .addComponent(variableButton)
                    .addGroup(mainPanelLayout.createSequentialGroup()
                        .addComponent(headerLabelsCheckBox)
                        .addGap(29, 29, 29)
                        .addComponent(columnLabelsCheckBox))
                    .addGroup(mainPanelLayout.createSequentialGroup()
                        .addComponent(labelsLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(labelsSeparator, javax.swing.GroupLayout.DEFAULT_SIZE, 347, Short.MAX_VALUE)
                            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap())
        );
        mainPanelLayout.setVerticalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(mainPanelLayout.createSequentialGroup()
                .addGap(20, 20, 20)
                .addComponent(columnCountSeparatorPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(10, 10, 10)
                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(mainPanelLayout.createSequentialGroup()
                        .addGap(3, 3, 3)
                        .addComponent(countLabel))
                    .addComponent(columnCountSpinner, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(mainPanelLayout.createSequentialGroup()
                        .addGap(3, 3, 3)
                        .addComponent(rowsLabel))
                    .addComponent(rowCountSpinner, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(columnWidthSeparatorPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(5, 5, 5)
                .addComponent(fixedButton)
                .addGap(5, 5, 5)
                .addComponent(fixedPercentButton)
                .addGap(5, 5, 5)
                .addComponent(variableButton)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(labelsSeparator, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(labelsLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(headerLabelsCheckBox)
                    .addComponent(columnLabelsCheckBox))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 15, Short.MAX_VALUE)
                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(okButton)
                    .addComponent(cancelButton))
                .addGap(17, 17, 17))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(mainPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(mainPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 295, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void cancel(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancel
        rc = CANCEL;
        setVisible(false);
        dispose();
    }//GEN-LAST:event_cancel

    private void ok(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ok
        rc = OK;
        setVisible(false);
        dispose();
    }//GEN-LAST:event_ok
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new InsertTableDialog(new javax.swing.JFrame(), true).setVisible(true);
            }
        });
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton cancelButton;
    private javax.swing.JLabel columnCountLabel;
    private javax.swing.JSeparator columnCountSeparator;
    private javax.swing.JPanel columnCountSeparatorPanel;
    private javax.swing.JSpinner columnCountSpinner;
    private javax.swing.JCheckBox columnLabelsCheckBox;
    private javax.swing.JLabel columnWidthLabel1;
    private javax.swing.JSeparator columnWidthSeparator1;
    private javax.swing.JPanel columnWidthSeparatorPanel;
    private javax.swing.ButtonGroup columnWidthTypeGroup;
    private javax.swing.JLabel countLabel;
    private javax.swing.JRadioButton fixedButton;
    private javax.swing.JRadioButton fixedPercentButton;
    private javax.swing.JCheckBox headerLabelsCheckBox;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JLabel labelsLabel;
    private javax.swing.JSeparator labelsSeparator;
    private javax.swing.JPanel mainPanel;
    private javax.swing.JButton okButton;
    private javax.swing.JSpinner rowCountSpinner;
    private javax.swing.JLabel rowsLabel;
    private javax.swing.JRadioButton variableButton;
    // End of variables declaration//GEN-END:variables
    
}
